<?php

namespace App\Exports;

use Illuminate\Database\Eloquent\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;

class ActivityExport implements FromCollection, WithEvents, ShouldAutoSize
{
    protected $data;

    //构造函数传值
    public function __construct($data)
    {
        $this->data = $data;
    }

    //数组转集合
    public function collection()
    {
        return new Collection($this->createData());
    }

    //业务代码
    public function createData()
    {
        return $this->data;
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {
                //设置行高，$i为数据行数
                //$event->sheet->getDelegate()->getColumnDimension('C')->setWidth(300);
                for ($i = 0; $i <= count($this->data); $i++) {
                    $event->sheet->getDelegate()->getRowDimension($i)->setRowHeight(30);
                }
            }];
    }

}
